package com.weiqiao.mall.repository.impl;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.result.UpdateResult;
import com.weiqiao.base.dao.impl.BaseMongoDaoImpl;
import com.weiqiao.mall.repository.IVarietyRepository;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 商品分类dao层
 *
 * @author PENGLIWEI
 * @createDate 2019-10-25
 */
@Repository("VarietyRepositoryImpl")
public class VarietyRepositoryImpl extends BaseMongoDaoImpl implements IVarietyRepository {

    private static final String COLLECTIONAME = "variety";

    @Override
    public List<String> selectList(Bson doc, Pageable pageable) {
        List<String> list = this.getListAndPage(COLLECTIONAME, doc, pageable);
        return list;
    }

    @Override
    public List<String> selectListByFilter(Bson filter) {
        List<String> list = this.findFilter(COLLECTIONAME, filter);
        return list;
    }

    @Override
    public List<String> selectCompanyList(Bson filter) {
        Bson sort = Filters.eq("variety_code", 1);
        return this.findFilterSort(COLLECTIONAME, filter, sort);
    }

    @Override
    public boolean saveVariety(String json) {
        Document doc = Document.parse(json);
        return this.inSertDocument(COLLECTIONAME, doc);
    }

    @Override
    public int getOrderLogCount(int company_id, Bson filter) {
        MongoCollection<Document> dbCollection = getCollection(COLLECTIONAME);
        return (int) dbCollection.count(filter);
    }

    @Override
    public boolean deleteById(Bson filter, Document doc, boolean bool) {
        MongoCollection<Document> dbCollection = getCollection(COLLECTIONAME);
        UpdateResult results = dbCollection.updateOne(filter, new Document("$set", doc),
                new UpdateOptions().upsert(bool).bypassDocumentValidation(true));
        if (results.getMatchedCount() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public String findVarietyFilter(Bson bson) {
        return this.findOne(COLLECTIONAME, bson);
    }

    @Override
    public boolean updateVariety(Bson filter, Document doc, boolean bool) {
        MongoCollection<Document> dbCollection = getCollection(COLLECTIONAME);
        UpdateResult results = dbCollection.updateOne(filter, new Document("$set", doc),
                new UpdateOptions().upsert(bool).bypassDocumentValidation(true));
        if (results.getMatchedCount() > 0) {
            return true;
        }
        return false;
    }

    /**
     * 根据查询条件和排序规则查询宝贝分类数据
     *
     * @param filter
     * @param sortFilter
     * @return
     */
    @Override
    public List<String> getVarietyListBySort(Bson filter, Bson sortFilter) {
        return super.findFilterSort(COLLECTIONAME, filter, sortFilter);
    }
}
